#include "stdafx.h"
#include "trend_param_list.h"

TrendParamList::TrendParamList(QWidget* pwgt) 
	: QDialog(pwgt, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
{
	ui.setupUi(this);
	signalMapper = new QSignalMapper(this);
	connect(signalMapper, SIGNAL(mapped(const QString &)), this, SLOT(cell_changed(const QString &)));

	connect(ui.btnTrend, SIGNAL(clicked), this, SLOT(on_btnTrend_clicked));
	connect(ui.btnCSV, SIGNAL(clicked), this, SLOT(on_btnCSV_clicked));
	connect(ui.bAdd, SIGNAL(clicked), this, SLOT(on_bAdd_clicked));
	connect(ui.cbAllParameters, SIGNAL(stateChanged(int)), this, SLOT(on_cbAllParameters_changed(int)));
	ui.cbAllParameters->setTristate(false);
	ui.table->setStyleSheet("alternate-background-color: rgb(230, 230, 230)");
	ui.table->setItemDelegate(new LineEditDelegate);

	ui.table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
	ui.table->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Fixed);

	ui.table->setColumnWidth(0, 18);
	ui.table->setColumnWidth(1, 120);
	ui.table->setColumnWidth(2, 160);
	ui.table->setColumnWidth(3, 60);
	ui.table->setColumnWidth(4, 25);
	
	//ui.table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
}

void TrendParamList::on_btnTrend_clicked(){
	isTrend = true;
	accept();
}

void TrendParamList::on_btnCSV_clicked(){
	isTrend = false;
	accept();
}

void TrendParamList::on_btnReject_clicked(){
	int result = QMessageBox::information(this, "Close", 
		"Close the form? any unsaved data will be lost", 
		QMessageBox::Yes | QMessageBox::Default, 
		QMessageBox::No, 
		QMessageBox::Cancel | QMessageBox::Escape); 
	if (result == QMessageBox::Yes) 
		reject();
}

void TrendParamList::setData(SceneView *_scene, vector<Common::trend_param>* param_list) {
	scene = _scene;
	ui.table->setRowCount(0);
	check.clear();
	item.clear();
	parameter.clear();
	unit.clear();
	itemList = scene->getAutItemsNameWithParameters();
	foreach(Common::trend_param trend, *param_list)
		add_row(trend.checked, trend.item, trend.parameter, trend.unit);
}

vector<Common::trend_param> TrendParamList::getData(){
	int count_trend = ui.table->rowCount();
	vector<Common::trend_param> new_trend_list;
	Common::trend_param new_trend;
	for (int i = 0; i < count_trend; ++i){
		new_trend.checked = check[i]->checkState();
		new_trend.item = scene->findAutItemByName(item[i]->currentText());
		new_trend.parameter = parameter[i]->currentText();
		new_trend.unit = unit[i]->currentText();
		new_trend_list.push_back(new_trend);
	}
	return new_trend_list;
}

void TrendParamList::on_cbAllParameters_changed(int value){
	if (value == 1) return;
	foreach(QCheckBox *cb, check)
		cb->setCheckState((Qt::CheckState)value);
}

void TrendParamList::on_check_changed(int value){
	int countChecked = 0, countUncheched = 0, index = 0;
	
	while (index < check.size() && (countChecked == 0 || countUncheched == 0) ){
		if (check[index]->checkState() == Qt::Unchecked)
			countUncheched++;
		else
			countChecked++;
		index++;
	}
	if (countChecked != 0 && countUncheched != 0){
		ui.cbAllParameters->setCheckState(Qt::PartiallyChecked);
	}else if (countChecked == 0){
		ui.cbAllParameters->setCheckState(Qt::Unchecked);
	}else if (countUncheched == 0){
		ui.cbAllParameters->setCheckState(Qt::Checked);
	}
	ui.btnTrend->setEnabled(countChecked);
	ui.btnCSV->setEnabled(countChecked);
}

void TrendParamList::update_item(){
 	int index = ui.table->currentRow();
 	update_cmb(item[index], scene->getAutItemsNameWithParameters());
}
/*

void TrendParamList::update_sensor(const int &index, const QString &_item_name){
 	QStringList sensorList = !_item_name.isEmpty() ? scene->getSensorsName(_item_name) : QStringList();
 	update_cmb(sensor[index], sensorList);
}
*/

void TrendParamList::update_parameter(const int &index, const QString &_item_name){
 	QStringList parameterList = !_item_name.isEmpty() ? scene->findAutItemByName(item[index]->currentText())->getCalculatedParameters() : QStringList();
 	update_cmb(parameter[index], parameterList);
}

void TrendParamList::update_unit(const int &index, const QString &_parameter_name){
 	update_cmb(unit[index], !_parameter_name.isEmpty() ? g_units.getUnitListForParameter(parameter[index]->currentText()) : QStringList());
}

void TrendParamList::update_cmb(QComboBox *cmb, const QStringList &list, const QString &current_text){
	cmb->clear();
	cmb->addItem("");
	if (list.isEmpty()) return;
	for (int j = 0; j < list.size(); j++)
		cmb->addItem(list[j]);
	int cur_index = -1;
	if (!current_text.isEmpty())
		for (int j = 0; j < list.size(); j++)
			if (list[j] == current_text){
				cur_index = j;
				break;
			}
	cmb->setCurrentIndex(cur_index != -1 ? cur_index + 1: (list.size() <= 1 ? cmb->count() - 1 : 0));
	cmb->setEnabled(list.size() > 1);
}

void TrendParamList::on_bAdd_clicked(){
	add_row(true, NULL, "", "");
}

void TrendParamList::add_row(const bool &_checked, automaticItem* _item, const QString &_parameter, const QString &_unit){
	int i = ui.table->rowCount();
	ui.table->setRowCount(i + 1);
	//check
	QCheckBox *cb = new QCheckBox;
	connect(cb, SIGNAL(stateChanged(int)), this, SLOT(on_check_changed(int)));
	check.push_back(cb);
	cb->setChecked(_checked);
	ui.table->setCellWidget(i, 0, cb);
	//item
	QComboBox *cmb_item = new QComboBox;
	update_cmb(cmb_item, itemList, _item != NULL ? _item->getName() : "");
	item.push_back(cmb_item);
	ui.table->setCellWidget(i, 1, cmb_item);
	connect(cmb_item, SIGNAL(currentIndexChanged(const QString&)), signalMapper, SLOT(map()));
	signalMapper->setMapping(cmb_item, QString("%1-%2").arg(1).arg(i));
	//parameter
	QComboBox *cmb_parameter = new QComboBox;
	QStringList parameterList = _item != NULL ? _item->getCalculatedParameters() : QStringList();
	update_cmb(cmb_parameter, parameterList, _parameter);
	parameter.push_back(cmb_parameter);
	ui.table->setCellWidget(i, 2, cmb_parameter);
	connect(cmb_parameter, SIGNAL(currentIndexChanged(const QString&)), signalMapper, SLOT(map()));
	signalMapper->setMapping(cmb_parameter, QString("%1-%2").arg(2).arg(i));
	//unit
	QComboBox *cmb_unit = new QComboBox;
	update_cmb(cmb_unit, g_units.getUnitListForParameter(_parameter), _unit);
	unit.push_back(cmb_unit);
	ui.table->setCellWidget(i, 3, cmb_unit);
	//delete btn
	QPushButton *btn_delete = new QPushButton(QIcon("img/remove.png"), "");
	connect(btn_delete, SIGNAL(clicked()), signalMapper, SLOT(map()));
	signalMapper->setMapping(btn_delete, QString("%1-%2").arg(0).arg(i));
	ui.table->setCellWidget(i, 4, btn_delete);
}

void TrendParamList::cell_changed(const QString &data){
	int col = data.split("-").front().toInt();
	int row = data.split("-").back().toInt();
	switch (col){
	case 1:
		update_parameter(row, item[row]->currentText());
		break;
	case 2:
		update_unit(row, parameter[row]->currentText());
		break;
	default:
		remove_row(row);
		break;
	}
}

void TrendParamList::remove_row( const int &index ){
	check.erase(check.begin() + index);
	item.erase(item.begin() + index);
	parameter.erase(parameter.begin() + index);
	unit.erase(unit.begin() + index);
	ui.table->removeRow(index);
}
